'** Created by Liberty BASIC Workshop - 6/24/2005 5:23:24 AM
'** Moving Items Between Listboxes
'this code copyright Alyce Watson
'use freely in your own programs
'no credit to author required
'please do not redistribute this code as is
'arrays used for listboxes avoid using index 0 for anything
'to keep array index in sync with listbox index
DIM a$(21) 'dim to one item larger than needed
a$(1) = "RED"
a$(2) = "GREEN"
a$(3) = "BLUE"
a$(4) = "YELLOW"
a$(5) = "ORANGE"
a$(6) = "BROWN"
a$(7) = "BLACK"
a$(8) = "WHITE"
a$(9) = "GRAY"
a$(10) = "PURPLE"
acount = 10 'current number of elements in a$ array
amax = 20 'maximum allowable elements in a$ array = dim size - 1
DIM b$(21) 'dim to one item larger than needed
b$(1) = "apple"
b$(2) = "orange"
b$(3) = "banana"
b$(4) = "peach"
b$(5) = "pear"
b$(6) = "grapes"
b$(7) = "kiwi"
b$(8) = "apricot"
b$(9) = "plum"
b$(10) = "melon"
bcount = 10 'current number of elements in b$ array
bmax = 20 'maximum allowable elements in b$ array = dim size - 1
[WindowSetup]
NOMAINWIN
WindowWidth = 401 : WindowHeight = 313
UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
[ControlSetup]
statictext #main.s, "DoubleClick or use buttons to move items.",26,10,250,24
stylebits #main.b1, _BS_MULTILINE, 0, 0, 0
button #main.b1, ">", [list1click], UL, 184, 60, 25, 30
stylebits #main.b2, _BS_MULTILINE, 0, 0, 0
button #main.b2, "<", [list2click], UL, 184, 105, 25, 30
listbox #main.list1, a$(),[list1click], 26, 50, 150, 210
listbox #main.list2, b$(),[list2click], 215, 50, 140, 210
Open "Moving Items Between Listboxes" for Window_nf as #main
#main "trapclose [quit]"
#main.list1 "selectindex 1"
#main.list2 "selectindex 1"
#main "font ms_sans_serif 10"
[loop]
Wait
[quit]
close #main : END
[list1click]
#main.list1 "selectionindex? sel"
if sel = 0 then wait 'if no item is selected, wait
#main.list1 "selection? selected$"
'only add an item if there is room in the array
if bcount < bmax then
bcount = bcount + 1 'increment item count
b$(bcount) = selected$ 'add item to b$ array
#main.list2 "reload" 'reload listbox
if acount > 0 then 'only remove item if there is at least one item left
for i = sel to acount
a$(i) = a$(i + 1) 'from sel to acount, move each item down one index
next
a$(acount) = "" 'remove item from last index
acount = acount - 1 'decrement count
#main.list1 "reload" 'reload listbox
end if
end if
wait
[list2click]
#main.list2 "selectionindex? sel"
if sel = 0 then wait 'if no item is selected, wait
#main.list2 "selection? selected$"
'only add an item if there is room in the array
if acount < amax then
acount = acount + 1 'increment item count
a$(acount) = selected$ 'add item to a$ array
#main.list1 "reload" 'reload listbox
if bcount > 0 then 'only remove item if there is at least one item left
for i = sel to bcount
b$(i) = b$(i + 1) 'from sel to bcount, move each item down one index
next
b$(bcount) = "" 'remove item from last index
bcount = bcount - 1 'decrement count
#main.list2 "reload" 'reload listbox
end if
end if
wait